<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <link href="../../component/pear/css/pear.css" rel="stylesheet"/>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <label class="layui-form-label">关键字</label>
                <div class="layui-input-inline">
                    <input type="text" name="keyWord" placeholder="类型名称\规格" class="layui-input">
                </div>
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="product-query">
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button type="reset" class="pear-btn pear-btn-md">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="table" lay-filter="product-filter"></table>
    </div>
</div>

</body>

<script type="text/html" id="record-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit">
        <i class="layui-icon layui-icon-edit"></i>
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove">
        <i class="layui-icon layui-icon-delete"></i>
    </button>
</script>

<script type="text/html" id="table-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
        <i class="layui-icon layui-icon-delete"></i>
        删除
    </button>
</script>

<script src="../../component/layui/layui.js"></script>
<script src="../../component/pear/pear.js"></script>
<script>
    layui.use(['table', 'form', 'common', 'easyAdmin'], function () {
        let easyAdmin = layui.easyAdmin;
        let MODULE_PATH = "product";
        let table = layui.table;
        let form = layui.form;
        let common = layui.common;
        let $ = layui.jquery;

        let cols = [
                [{
                    type: 'checkbox'
                },
                    {
                        title: '商品序号',
                        field: 'commId',
                        align: 'center'
                    },
                    {
                        title: '商品类型',
                        field: 'commTypeName',
                        align: 'center'
                    },
                    {
                        title: '商品名称',
                        field: 'commName',
                        align: 'center'
                    },
                    {
                        title: '商品单价',
                        field: 'commPrice',
                        align: 'center'
                    },
                    {
                        title: '商品规格',
                        field: 'commSpecs',
                        align: 'center'
                    },
                    {
                        title: '计量单位',
                        field: 'commUnit',
                        align: 'center'
                    },
                    {
                        title: '照片缩略图',
                        field: 'commPicUri',
                        align: 'center',
                        templet: function (d) {
                            var url = d.commPicUri;
                            return '<div style="width:30px;height:30px;border: 1px solid rgb(200, 200, 200);border-radius: 50%;overflow:hidden;"><img src="' + url + '" style="width:30px;height:30px;cursor: pointer;" lay-event="prevPic" title="点击查看" /></div>';
                        }
                    },
                    {
                        title: '创建时间',
                        field: 'createdTime',
                        align: 'center'
                    },
                    {
                        title: '操作',
                        toolbar: '#record-toolbar',
                        align: 'center'
                    }
                ]
            ]
        ;

        easyAdmin.tableRender({
            elem: '#table', // 对应table的id
            url: '/comm/product',
            page: true, // 分页参数可以自定义
            cols: cols, // 列表示
            skin: 'line', // 表格样式
            toolbar: '#table-toolbar', // 表格头部工具栏 可定义左上角和右上角工具栏,一般用于左上角配置
            defaultToolbar: [{  // 自由配置头部工具栏右侧的图标按钮
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
        });

        // toolbar: '#toolbarDemo'
        // toolbar 点击头部工具栏区域设定了属性为 lay-event="" 的元素时触发(包括左上角和右上角)
        table.on('toolbar(product-filter)', function (obj) {
            switch (obj.event) {
                case 'add':
                    ProductAdd(MODULE_PATH);
                    break;
                case 'refresh':
                    easyAdmin.tableRefresh('table');
                    break;
                case 'batchRemove':
                    window.batchRemove(obj);
                    break;
            }
        });

        table.on('tool(product-filter)', function (obj) {
            switch (obj.event) {
                case 'remove':
                    window.remove(obj);
                    break;
                case 'edit':
                    ProductEdit(MODULE_PATH, obj.data.commId);
                    break;
                case 'prevPic':
                    prevPicOpen(obj);
                    break;
            }
        });

        /**
         * 跳转到add页面
         */
        let ProductAdd = function (MODULE_PATH) {
            layer.open({
                type: 2,
                title: '新增',
                shade: 0.1,
                area: [common.isModile() ? '100%' : '800px', common.isModile() ? '100%' : '560px'],
                content: MODULE_PATH + '/add.html'
            });
        }

        /**
         * 跳转到edit页面
         */
        let ProductEdit = function (MODULE_PATH, id) {
            layer.open({
                type: 2,
                title: '修改',
                shade: 0.1,
                area: ['800px', '660px'],
                content: [MODULE_PATH + '/edit.html?id=' + id, 'no']
            });
        }

        let prevPicOpen = function (obj) {
            var imgSrc = obj.data.commPicUri;
            console.log(imgSrc)
            if (imgSrc != "") {
                // 图片预览
                layer.open({
                    type: 1,
                    title: false,
                    closeBtn: 0,
                    area: 'auto',
                    skin: 'layui-layer-nobg', // 没有背景色的class
                    shadeClose: true,
                    content: "<img src='" + imgSrc + "' style='margin: auto; max-width: 100%; : 100%;max-height'>"
                });
            }
        }



        window.remove = function (obj) {
            layer.confirm('确定要删除该商品', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                easyAdmin.http({
                    url: "/comm/product/" + obj.data['commId'],
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000,
                                area: ['100px', '80px'],
                                content: "删除成功"
                            }, function () {
                                obj.del();
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000,
                                area: ['260px', '80px']
                            });
                        }
                    }
                })
            });
        }

        window.batchRemove = function (obj) {
            var checkIds = common.checkField(obj, 'commId');
            if (checkIds === "") {
                layer.msg("未选中数据", {
                    icon: 3,
                    time: 1000
                });
                return false;
            }

            layer.confirm('确定要删除这些商品', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                easyAdmin.http({
                    url: "/comm/product/batch/" + checkIds,
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function () {
                                table.reload('table');
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                area: ['260px', '80px'],
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        // 触发submit提交 按钮点击或者表单被执行提交时触发
        form.on('submit(product-query)', function (data) {
            table.reload('table', {
                // //设定异步数据接口的额外参数
                where: data.field
            })
            return false; // 阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
    })
</script>
</html>
